<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Checklist_m extends CI_Model {
        
        function get_all_category() {
            $q = $this->db->select('*')->from('category')
                    ->order_by('category_id', 'DESC')->get();
            if($q->num_rows() > 0) {
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_category($cat_id){
            $q = $this->db->where('category_id', $cat_id)->get('category');
            if($q->num_rows() > 0) {
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function get_sub_cats($id) { 
            $q = $this->db->where('category_id', $id)->get('checklist');
            if($q->num_rows() > 0) {
                return $q->result_array();
            }else{
                return FALSE;
            }
        }
        
        function get_sub_cat($id){
            $q = $this->db->where('checklist_id', $id)->get('checklist');
            if($q->num_rows() > 0) {
                return $q->row();
            }else{
                return FALSE;
            }
        }
        
        function check_if_cat_exist($name){
            $q = $this->db->where('category_name', $name)->get('category');
            if($q->num_rows() > 0) {
                return 'exist';
            }else{
                return 'dont exist';
            }
        }
        
        function check_if_sub_cat_exist($name, $id){
            $q = $this->db->where('list_description', $name)->where('category_id', $id)->get('checklist');
            if($q->num_rows() > 0) {
                return 'exist';
            }else{
                return 'dont exist';
            }
        }
        
        function insert_cat($cat_name) {
            $this->db->insert('category', $cat_name);
            return $this->db->insert_id();
        }
        
        function insert_sub_cat($data) {
            $this->db->insert('checklist', $data);
            return $this->db->insert_id();
        }
        
        function delete_sub_cat($id){
            $this->db->where('checklist_id', $id)->delete('checklist');
            return $this->db->affected_rows();
        }
        
        function delete_cat($id) {
            $this->db->where('category_id', $id)->delete('category');
            return $this->db->affected_rows();
        }
    }
